int mbid = 0;
int npcid = 0;
int blkid = 0;
int coinid = 0;

struct Mbrush
{
    float x, y;
    float max_low, max_high;
    int blk, dir, brad, mrad, speed, akdir;
};

struct Npc
{
    float x, y;
    int type, sdir, speed;
};

struct Blk
{
    float x, y;
    int blk, type, value, area, master;
};

struct Banana
{
    float x, y;
    int area;
};

struct Mapset
{
    float spawn_x, spawn_y, finish_x, finish_y;
    int bgtex;
};

Mbrush Rmb[512];
Npc Rnpc[512];
Blk Rblk[512];
Banana Rcon[512];
Mapset Rmap;

void mcResetMap(void)
{
    mbid = 0;
    blkid = 0;
    npcid = 0;

    if(mbid > 0)
        free(Rmb);
    if(npcid > 0)
        free(Rnpc);
    if(blkid > 0)
        free(Rblk);
    if(coinid > 0)
        free(Rcon);
}

int GetArea(float x)
{
    float pos = -1;
    int area = 0;
    
    while(pos < x)
    {
        area++;
        pos += 0.5f;
    }
    
    return area;
}

void mcLoadMap(int lvl)
{
    //mcResetMap();
    FILE *file;
    long ls;
    char *buf;
    char map[18]; sprintf(map,"src/maps/test.map"); //%d , lvl

    file = fopen (map, "rb");
    if(file == NULL)
        mcError("Error: Couldn't load the mapfile");

    fseek(file , 0 , SEEK_END);
    ls = ftell(file);
    rewind(file);

    buf = (char*) malloc(ls);
    if(buf == NULL)
        mcError("Error: Couldn't load the mapfile");

    fread(buf,1,ls,file);
    fclose(file);

    //GET THE LINES
    int index = 0;
    char *lines[512];
    char *token;
    token = strtok(buf,"\n");
    while(token != NULL)
    {
        if(index > 510)
            break;
        lines[index] = token; index++;
        token = strtok(NULL,"\n");
    }

    //GET THE CONTENT OF THE LINES
    char *token2;
    int index2 = 0;

    for(int i = 0; i < index-1; i++)
    {
        index2 = 0;
        char *values[9];

        token2 = strtok(lines[i],"#");
        while(token2 != NULL)
        {
            values[index2] = token2; index2++;
            token2 = strtok(NULL,"#");
        }

        int ltyp = 0;
        sscanf(values[0],"%d",&ltyp);
        
        switch(ltyp)
        {
            case 0:
                sscanf(values[1], "%f", &Rmb[mbid].x);
                sscanf(values[2], "%f", &Rmb[mbid].y);
                sscanf(values[3], "%d", &Rmb[mbid].blk);
                sscanf(values[4], "%d", &Rmb[mbid].dir);
                sscanf(values[5], "%d", &Rmb[mbid].brad);
                sscanf(values[6], "%d", &Rmb[mbid].mrad);
                sscanf(values[7], "%d", &Rmb[mbid].speed);
                break;
                Rmb[mbid].akdir = 0;
                
                /*if(true)
                {
                    Rmb[mbid].max_low = Rmb[mbid].x - Rmb[mbid].mrad * 22 * wpx;
                    Rmb[mbid].max_high = Rmb[mbid].x + Rmb[mbid].mrad * 22 * wpx; 
                *///} else {
                //Rmb[mbid].max_low = Rmb[mbid].x - Rmb[mbid].mrad * 22 * wpx;
                //Rmb[mbid].max_high = Rmb[mbid].x + Rmb[mbid].mrad * 22 * wpx; 
                //}
                //Rmb[mbid].max_low = Rmb[mbid].x;
                //Rmb[mbid].max_high = Rmb[mbid].x;
                
                Rblk[blkid].x = Rmb[mbid].x;
                Rblk[blkid].y = Rmb[mbid].y;
                Rblk[blkid].blk = 5;
                Rblk[blkid].type = 0;
                Rblk[blkid].value = 0;
                Rblk[blkid].area = GetArea(Rblk[blkid].x);
                Rblk[blkid].master = mbid+1;
                blkid++;
                
                for(int i = 1; i <= Rmb[mbid].brad+1; i++)
                {
                    if(Rmb[mbid].dir == 0) //RIGHT-LEFT
                    {
                        Rblk[blkid].x = Rmb[mbid].x + i * 22 * wpx;
                        Rblk[blkid].y = Rmb[mbid].y;
                        Rblk[blkid].blk = 5;
                        Rblk[blkid].type = 0;
                        Rblk[blkid].value = 0;
                        Rblk[blkid].area = GetArea(Rblk[blkid].x);
                        Rblk[blkid].master = mbid+1;
                        blkid++;
                        
                        Rblk[blkid].x = Rmb[mbid].x - i * 22 * wpx;
                        Rblk[blkid].y = Rmb[mbid].y;
                        Rblk[blkid].blk = 5;
                        Rblk[blkid].type = 0;
                        Rblk[blkid].value = 0;
                        Rblk[blkid].area = GetArea(Rblk[blkid].x);
                        Rblk[blkid].master = mbid+1;
                        blkid++;
                    } else {
                        
                    }
                }
                
                mbid++;
                break;
            case 1:
                sscanf(values[1], "%f", &Rblk[blkid].x);
                sscanf(values[2], "%f", &Rblk[blkid].y);
                sscanf(values[3], "%d", &Rblk[blkid].blk);
                sscanf(values[4], "%d", &Rblk[blkid].type);
                sscanf(values[5], "%d", &Rblk[blkid].value);
                Rblk[blkid].master = 0;
                Rblk[blkid].area = GetArea(Rblk[blkid].x);
                blkid++;
                break;
            case 2:
                sscanf(values[1], "%f", &Rcon[coinid].x);
                sscanf(values[2], "%f", &Rcon[coinid].y);
                Rcon[coinid].area = GetArea(Rcon[coinid].x);
                coinid++;
                break;
            case 3:
                sscanf(values[1], "%f", &Rmap.spawn_x);
                sscanf(values[2], "%f", &Rmap.spawn_y);
                break;
            case 4:
                sscanf(values[1], "%f", &Rmap.finish_x);
                sscanf(values[2], "%f", &Rmap.finish_y);
                break;
            case 5:
                sscanf(values[1], "%f", &Rnpc[npcid].x);
                sscanf(values[2], "%f", &Rnpc[npcid].y);
                sscanf(values[3], "%d", &Rnpc[npcid].type);
                sscanf(values[4], "%d", &Rnpc[npcid].sdir);
                sscanf(values[5], "%d", &Rnpc[npcid].speed);
                npcid++;
                break;
            case 6:
                sscanf(values[1], "%d", &Rmap.bgtex);
                break;
            default:
                break;
        }
    }
    
    Monkey.Pos.x = Rmap.spawn_x;
    Monkey.Pos.y = Rmap.spawn_y;
    Monkey.State = MONKEY_NORM;
    Monkey.Lives = 2;
}